from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait



driver = webdriver.Chrome()
driver.get('http://localhost/shop/index.php?controller=simple&action=login')



# 制作用户名输入框 定位器
username_locator = (By.NAME,'login_info')
username = WebDriverWait(driver,10,1).until(EC.presence_of_element_located(username_locator))
username.send_keys('ceshi2001')

# 定位密码输入框
password_locator = (By.NAME,'password')
password = WebDriverWait(driver,10,1).until(EC.presence_of_element_located(password_locator))
password.send_keys('ceshi2001')

# 点击登录
submit_locator = (By.CLASS_NAME,'submit_login')
submit = WebDriverWait(driver,10,1).until(EC.presence_of_element_located(submit_locator))
submit.click()

# 判断结果是否符合预期
expected_url = 'http://localhost/shop/index.php?controller=ucenter&action=index'
ret_url = driver.current_url
try:
    result_locator = (By.CLASS_NAME, 'f14')
    result = WebDriverWait(driver, 10, 1).until(EC.presence_of_element_located(result_locator))
    # if ret_url == expected_url and result.text == '您好，ceshi2001 欢迎回来!':
    #     print('登录成功')
    # 三目运算符
    print('登录成功') if ret_url == expected_url and result.text == '您好，ceshi2001 欢迎回来!' else print('登录失败')

except:
    print('用户名或密码错误，登录没有成功')


driver.quit()





